//
//  KLDPDetViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/9/2.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "KLDPDetViewController.h"

#import "GXDPView.h"
#import "PinJiaCell.h"
#import "PingJiaViewController.h"
#import "KuLiViewController.h"
#import "KuPersonViewController.h"
#import "LoginViewController.h"
#import "NavViewController.h"
#import "KuLiCell.h"
#import "SDCycleScrollView.h"
@interface KLDPDetViewController ()<UITableViewDataSource,UITableViewDelegate,SDCycleScrollViewDelegate>{
    NSMutableArray *array;
    KuLiCell *cell;
}

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSDictionary *mdic;
//
@property (nonatomic,strong) NSIndexPath *lastPath;

@property (nonatomic,strong) NSMutableArray *choseArray;

@property (nonatomic,strong) NSMutableArray *datas;

@end

@implementation KLDPDetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self showNav:@"店铺详情"];
    array = [NSMutableArray array];
    self.choseArray = [NSMutableArray array];
    self.datas = [NSMutableArray array];
    [self loadUI];
    [self loadData];
        
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-50) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self.view addSubview:_tableView];
    
    //头视图
    GXDPView *view = [[[NSBundle mainBundle] loadNibNamed:@"GXDPView" owner:nil options:nil] lastObject];
    view.frame = CGRectMake(0, 0, ScreenWidth, 300);
    _tableView.tableHeaderView = view;
    
    view.labName.text = [_dic objectForKey:@"name"];
    if ([[_dic objectForKey:@"profile"] isEqualToString:@""]) {
        
        view.frame = CGRectMake(0, 0, ScreenWidth, 270);

    }else
    {
        view.frame = CGRectMake(0, 0, ScreenWidth, 300);
        
    }
    view.labdet.text = [_dic objectForKey:@"profile"];
    view.labPlace.text = [NSString stringWithFormat:@"地址：%@",[_dic objectForKey:@"suite"]];
    view.labOrder.text = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"order"] integerValue]];
    
    NSString *images = [_dic objectForKey:@"figurepic"];
    NSArray *arr = [images componentsSeparatedByString:@"|"];
    NSMutableArray *imageDatas = [NSMutableArray array];
    for (NSString *str in arr) {
        NSString *imageurl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],str];
        [imageDatas addObject:imageurl];
    }
    
//    UIView *v = [WTImageScroll ShowNetWorkImageScrollWithFream:CGRectMake(0, 0, ScreenWidth, 170) andImageArray:imageDatas andBtnClick:^(NSInteger tagValue) {
//
//    }];
//    view.images.userInteractionEnabled = YES;
//    [view.images addSubview:v];
    
    //用这个轮播吧
    view.images.userInteractionEnabled = YES;
    SDCycleScrollView *cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, ScreenWidth, 170) delegate:self placeholderImage:[UIImage imageNamed:@""]];
    [view.images addSubview:cycleView];
    cycleView.imageURLStringsGroup = imageDatas;
    
    NSString *start = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"star"] integerValue]];
    CGFloat count = [start floatValue];
    StarView *starView = [[StarView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    starView.showStar = count * 20;
    [view.starView addSubview:starView];
    
    //尾
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-50, ScreenWidth, 50)];
    footView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footView];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.5)];
    line.backgroundColor =  COLOR_RGB(231, 231, 231, 1);
    [footView addSubview:line];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(ScreenWidth/4, 7, ScreenWidth/2, 36);
    btn.layer.cornerRadius = 18.0;
    btn.backgroundColor = COLOR_RGB(253, 17, 66, 1);
    [btn setTitle:@"呼叫苦力" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickTEL) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:btn];
    
    
}

#pragma mark -- clickCycle
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    //这里要是点击轮播图做一些事件就在这里处理
}

//呼叫苦力
- (void)clickTEL{
    if (![user objectForKey:@"userid"]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        NavViewController *nav = [[NavViewController alloc] initWithRootViewController:login];
        nav.navigationBarHidden = YES;
        [self presentViewController:nav animated:YES completion:nil];
        return;
    }
    
    if (self.datas.count<=0) {
        
        [LICGToast showWithText:@"请选择苦力人员"];
        return;
    }
    KuLiViewController *kuli = [[KuLiViewController alloc] init];
    kuli.merid = _merid;
    kuli.name = [_dic objectForKey:@"name"];
    kuli.array = self.datas;
    [self.navigationController pushViewController:kuli animated:YES];
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"KuLiCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = array[indexPath.row];
    cell.labName.text = [dic objectForKey:@"name"];
    NSString *age = [NSString stringWithFormat:@"%li岁",[[dic objectForKey:@"age"] integerValue]];
    NSString *seniority = [NSString stringWithFormat:@"%li年",[[dic objectForKey:@"seniority"] integerValue]];
    NSString *order = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"order"] integerValue]];
    cell.labAge.text =age;
    cell.labYear.text = seniority;
    cell.labOrder.text = order;
    
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"img"]];
    [cell.imageHead sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"Avatar-sample-187"]];
    
    NSString *star = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"star"] integerValue]];
    NSInteger count = [star integerValue];
    StarView *starView = [[StarView alloc] initWithFrame:CGRectMake(0, 0, 80, 20)];
    starView.showStar = count * 20;
    [cell.starView addSubview:starView];
    

    //字符串
    NSString*  selectRow  = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    //数组中包含当前行号，设置对号
    if ([self.choseArray  containsObject:selectRow]) {
        cell.imageSel.image = [UIImage imageNamed:@"勾1"];
    }else{
        cell.imageSel.image = [UIImage imageNamed:@"圆角矩形-1"];
    }
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self
                                                                         action:@selector(tapImageViewWithTag:)];
    cell.imageSel.userInteractionEnabled = YES;
    [cell.imageSel addGestureRecognizer:tap];
    
    UIView *singleTapView = [tap view];
    singleTapView.tag = indexPath.row;
    
    return cell;
}

- (void)tapImageViewWithTag:(id)sender{
    UITapGestureRecognizer *singleTap = (UITapGestureRecognizer *)sender;
    NSInteger _tag = [singleTap view].tag;
    NSString*  selectRow  =  [NSString stringWithFormat:@"%ld",(long)_tag];
    //判断数组中有没有被选中行的行号,
    if ([self.choseArray containsObject:selectRow]) {
        [self.choseArray removeObject:selectRow];
    }else{
        [self.choseArray addObject:selectRow];
    }
    
    //cell刷新
//    NSIndexPath *index=[NSIndexPath indexPathForRow:tag inSection:0];
//    [_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:index,nil] withRowAnimation:UITableViewRowAnimationNone];
    
    [_tableView reloadData];
    
    NSDictionary *dic = array[_tag];
    if ([self.datas containsObject:dic]) {
        [self.datas removeObject:dic];
    }else{
        [self.datas addObject:dic];
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *blue = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, 4, 20)];
    blue.backgroundColor = COLOR_RGB(17, 100, 253, 1);
    [view addSubview:blue];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(28, 10, 100, 20)];
    lab.text = @"选择苦力";
    lab.font = [UIFont systemFontOfSize:14.0];
    [view addSubview:lab];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 39, ScreenWidth, 0.5)];
    line.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [view addSubview:line];
    
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    KuPersonViewController *man = [[KuPersonViewController alloc] init];
    NSDictionary *dics = array[indexPath.row];
    man.dic = dics;
    [self.navigationController pushViewController:man animated:YES];
    
//    NSString*  selectRow  = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
//    //判断数组中有没有被选中行的行号,
//    if ([self.choseArray containsObject:selectRow]) {
//        [self.choseArray removeObject:selectRow];
//    }else{
//        [self.choseArray addObject:selectRow];
//    }
//    
//    //cell刷新
//    NSIndexPath *index=[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
//    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:index,nil] withRowAnimation:UITableViewRowAnimationNone];
//    
//    NSDictionary *dic = array[indexPath.row];
//    if ([self.datas containsObject:dic]) {
//        [self.datas removeObject:dic];
//    }else{
//        [self.datas addObject:dic];
//    }
    
}

//苦力-用户成员
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_merid forKey:@"merchantid"];
    NSString *url = [NSString stringWithFormat:@"%@/App/Coolie/cxcoolimember",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"苦力-用户成员res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            array = [responseObject objectForKey:@"result"];
            [_tableView reloadData];
        }
        
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
